<template>
  <div class="app-container">
    <div class="card-box">
      <el-button type="primary" @click="handleToAdd">添加区域代理</el-button>
    </div>
    <div class="card-box">
      <el-table
        v-loading="regionalAgentStore.loading.list || regionalAgentStore.loading.update"
        :data="regionalAgentStore.list"
        row-id="id"
      >
        <el-table-column label="ID" prop="id" />
        <el-table-column label="名称" prop="name" />
        <el-table-column label="用户">
          <template #default="{ row }">
            {{ row.user.username }}
          </template>
        </el-table-column>
        <el-table-column label="省">
          <template #default="{ row }">
            {{ row.province ? row.province.name : "" }}
          </template>
        </el-table-column>
        <el-table-column label="市">
          <template #default="{ row }">
            {{ row.city ? row.city.name : "" }}
          </template>
        </el-table-column>
        <el-table-column label="区">
          <template #default="{ row }">
            {{ row.district ? row.district.name : "" }}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template #default="{ row }">
            <el-button type="primary" link @click="handleToEdit(row)">编辑</el-button>
            <el-popconfirm title="是否继续删除？" @confirm="handleDelete(row)">
              <template #reference>
                <el-button type="danger" link>删除</el-button>
              </template>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        v-model:current-page="params.page"
        v-model:page-size="params.size"
        style="margin-top: 10px"
        background
        :total="regionalAgentStore.totalCount"
        @change="regionalAgentStore.fetchList(params)"
      />
    </div>
    <Form
      v-model="visible"
      :type="formType"
      :form="currentForm"
      @confirm="regionalAgentStore.fetchList(params)"
    />
  </div>
</template>

<script setup lang="ts">
import {
  type RegionalAgentListQuery,
  type RegionalAgentForm,
  type RegionalAgentItemType,
} from "@/api/user/regionalAgent";
import { useRegionalAgentStoreHook } from "@/store/modules/user/regional-agent";
import Form from "./components/form.vue";
import type { FormType } from "./components/form.vue";

const regionalAgentStore = useRegionalAgentStoreHook();

const visible = ref(false);

const formType = ref<FormType>("add");

const currentForm = ref<RegionalAgentForm>({
  id: 0,
  name: "",
  user: "",
  province: 0,
  city: 0,
  district: 0,
});

const params = reactive<RegionalAgentListQuery>({
  page: 1,
  size: 10,
});

function handleToAdd() {
  formType.value = "add";
  visible.value = true;
}

async function handleDelete(row: RegionalAgentItemType) {
  await regionalAgentStore.fetchDelete(row.id);
  await regionalAgentStore.fetchList(params);
}

function handleToEdit(row: RegionalAgentItemType) {
  formType.value = "edit";
  currentForm.value = {
    id: row.id,
    name: row.name,
    user: row.user.id as unknown as string,
    province: row.province ? row.province.id : undefined,
    city: row.city ? row.city.id : undefined,
    district: row.district ? row.district.id : undefined,
  };
  visible.value = true;
}

onMounted(() => {
  regionalAgentStore.fetchList(params);
});
</script>

<style scoped lang="scss"></style>
